const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 过滤html标签
const filterContent = (content) => {
  return content.replace(/<[^<>]+>/g,'')
}

// 获取图片链接
const filterImages = (content) => {
  let arr = []
  content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, function (match, capture) {
    arr.push(capture);
  });
  return arr
}

// 判断活动时间是否有效
const computedTimeStatus = (startTime, endTime) => {
  let start = Date.parse(startTime) || Date.parse(startTime.replace(/-/g, '/'));
  let end = Date.parse(endTime) || Date.parse(endTime.replace(/-/g, '/'));
  let now = +new Date();
  if(start > now){
    return '未开始';
  } else if(end < now){
    return '已结束';
  } else {
    return '进行中';
  }
}

// 校验邮箱合法性
const checkEmail = (value) => {
  var reg=/^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/;
  return reg.test(value);
}

const sliceDate = (date) => {
  return date.slice(0, 11);
}

module.exports = {
  formatTime,
  filterContent,
  filterImages,
  sliceDate,
  computedTimeStatus,
  checkEmail
}